package com.sharecode.stock.strategy.process.macd;

import java.util.List;

import org.apache.commons.collections4.CollectionUtils;

import com.sharecode.stock.strategy.process.Process;
import com.sharecode.stock.strategy.vo.Energy;
import com.sharecode.stock.strategy.vo.MACD;
import com.sharecode.stock.strategy.vo.StrategyVo;
import com.sharecode.stock.utils.IDUtils;
import com.sharecode.stock.vo.stock.StockHistory;

import lombok.extern.slf4j.Slf4j;

/**
 * macd预处理
 * 
 * @author Administrator
 *
 */
@Slf4j
public class MacdProcess implements Process {

	private static final MacdProcess MACDPROCESS = new MacdProcess();

	public static MacdProcess getInstance() {
		return MACDPROCESS;
	}

	/**
	 * 预处理
	 * 
	 * @param vo
	 */
	@Override
	public void process(StrategyVo vo) {
		List<MACD> macd = vo.getMacd();
		List<StockHistory> stockHistories = vo.getStockHistories();
		int size = macd.size();
		int k1 = size - 1;
		int k2 = size - 2;
		Double e1 = macd.get(k1).getMacd();
		Double e2 = macd.get(k2).getMacd();

		StockHistory stockHistory = stockHistories.get(k1);
		double p1 = stockHistory.getClose().doubleValue();

		List<Energy> energys = vo.getEnergys();
		if (CollectionUtils.isEmpty(energys)) {
			changeEnergy(vo);
			return;
		}

		Energy energy = vo.getEnergys().get(vo.getEnergys().size() - 1);

		if ((e1 >= 0 && e2 < 0) || (e1 < 0 && e2 >= 0)) {// 0轴由下往上 0轴由上往下
			changeEnergy(vo);
		} else {
			if (e1 >= 0 && e2 >= 0) {// 0轴上方
				if (p1 > energy.getMaxPrice()) {
					energy.setMaxPrice(p1);
				}
				if (e1 > energy.getMaxEnergy()) {
					energy.setMaxEnergy(e1);
				}
			} else if (e1 <= 0 && e2 <= 0) {// 0轴下方
				if (p1 < energy.getMaxPrice()) {
					energy.setMaxPrice(p1);
				}
				if (e1 < energy.getMaxEnergy()) {
					energy.setMaxEnergy(e1);
				}
			} else {
				log.error("p1价格:" + e1 + " p2价格:" + e2);
			}
			energy.getStockHistories().add(stockHistory);
			energy.getMacds().add(macd.get(k1));
		}

	}

	private static void changeEnergy(StrategyVo vo) {
		Energy energy = new Energy();
		energy.setId(IDUtils.id());
		energy.getStockHistories().add(vo.getStockHistories().get(vo.getStockHistories().size() - 1));
		energy.getMacds().add(vo.getMacd().get(vo.getMacd().size() - 1));
		vo.getEnergys().add(energy);
	}

}
